<template>
  <div class="app-container">
    <div style="text-align: center">{{createStatus?'添加':'更新'}}维保考核信息</div>
    <div style="text-align: center;margin-bottom: 30px">
    </div>
    <div style="width: 50%;margin:0px auto;vertical-align:middle;height:100%">
      <el-form :model="checkRecordsFrom" ref="checkRecordsFrom" :rules="checkRecordsFromRules" label-width="100px">
        <el-form-item label="责任企业" prop="enterpriseId" >
          <el-select v-model="checkRecordsFrom.enterpriseId" placeholder="请选择责任企业" style="width: 100%" v-if="enterPriseHidden">
            <el-option v-for="(item,index) in enterpriseList" :key="index"  :value="item.ucId" :label="item.name" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="责任人" prop="executor" >
          <el-select v-model="checkRecordsFrom.executor" placeholder="请选择责任人" style="width: 100%">
            <el-option v-for="(item,index) in executorList" :key="index"  :value="item.ucId" :label="item.name" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="考核记录描述" prop="remark">
          <el-input v-model="checkRecordsFrom.remark" placeholder="请输入维保详情" style="width: 100%"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" style="text-align: center" class="dialog-footer">
      <el-button @click="cancelCommission">取消</el-button>
      <el-button :loading="addLoading" v-if="createStatus" type="primary" @click="addInCheckRecords">创 建
      </el-button>
      <el-button :loading="addLoading" v-else type="primary" @click="updateInCheckRecords">更新</el-button>
    </div>
  </div>
</template>

<script>

import {getInEnterpriseDominantList} from "../../../api/enterpriseDominant";
import {getInUserList} from "../../../api/enterprise";
import {addInCheckRecords, getInCheckRecordsVo, updateInCheckRecords} from "@/api/maintenanceCheckRecords";
export default {
  name: "edit",
  data(){
    return{
      checkRecordsFrom:{

      },
      checkRecordsFromRules:{
        // designee: [{ required: true, message: '请选择指定人', trigger: 'blur' }],
        enterpriseId: [{ required: true, message: '请选择企业', trigger: 'blur' }],
        // equipmentId: [{ required: true, message: '请选择设备', trigger: 'blur' }],
      },
      addLoading:false,
      enterpriseList:[],
      executorList:[]
    }
  },
  computed: {
    createStatus(){
      return this.$route.query.ucId==undefined
    },
    enterPriseHidden(){
      let roleCode = this.$store.getters.roleCode;
      return roleCode=="ADMIN"||roleCode=="XT-ADMIN"||roleCode=="ZD-ADMIN"||roleCode=="DD-ADMIN"||roleCode=="JW-ADMIN"||roleCode=="WB-ADMIN"
    }
  },
  created() {
    if(!this.createStatus){
      let ucId =this.$route.query.ucId
      console.log(ucId)
      this.getInCheckRecordsVo(ucId)
    }
    this.getInEnterpriseDominantList();
    this.getInUserList();
  },
  methods:{
    cancelCommission(){
      window.history.back()
    },
    getInCheckRecordsVo(val) {
      getInCheckRecordsVo({ucId: val}).then(res =>{
        console.log(res)
        this.checkRecordsFrom = res.data
      }).catch(res => {
        this.addLoading = false
        this.$notify.error({
          title: '失败',
          message: res.msg
        })
      })
    },
    addInCheckRecords() {
      this.addLoading = true
      this.$refs['checkRecordsFrom'].validate(valid => {
        if (valid) {
          addInCheckRecords(this.checkRecordsFrom).then(res => {
            this.addLoading = false
            this.$notify.success({
              title: '成功',
              message: "添加维保信息成功"
            })
            this.cancelCommission()
          }).catch(res => {
            this.addLoading = false
            this.$notify.error({
              title: '失败',
              message: res.msg
            })
          })
        } else {
          this.addLoading = false
          console.log("表单验证不通过")
        }
      })
    },
    updateInCheckRecords() {
      this.addLoading = true
      this.$refs['checkRecordsFrom'].validate(valid => {
        if (valid) {
          updateInCheckRecords(this.checkRecordsFrom).then(res => {
            this.addLoading = false
            this.$notify.success({
              title: '成功',
              message: "更新维保记录信息成功"
            })
            this.cancelCommission()
          }).catch(res => {
            this.addLoading = false
            this.$notify.error({
              title: '失败',
              message: res.msg
            })
          })
        } else {
          this.addLoading = false
          console.log("表单验证不通过")
        }
      })
    },
    getInEnterpriseDominantList() {
      getInEnterpriseDominantList({}).then(res => {
        this.enterpriseList=res.data;
      }).catch(res => {
        this.addLoading = false
        this.$notify.error({
          title: '失败',
          message: res.msg
        })
      })
    },
    getInUserList() {
      getInUserList({}).then(res => {
        this.executorList = res.data
        console.log(res.data)
      }).catch(res => {
        this.addLoading = false
        this.$notify.error({
          title: '失败',
          message: res.msg
        })
      })
    },

  }

}
</script>

<style
    scoped>

</style>